﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Renderers.Core;

namespace NetJsWire.Widgets.Renderers.ToolBar
{
    [ ClassName( "Ext.menu.Item" ) ]
    internal class MenuItem : Widget, IRenderer
    {
        private readonly Ui.ToolBar.MenuItem _mi;
        protected string _CHECKEDCHANGED = "function(i, c) {{ SendToServer(\"{0}\", \"CheckedChanged\", \"{1}\", c); }}";
        protected string _CHECKEDCHANGED_CLIENT_NAME = "checkchange";
        protected string _CLICK = "function() {{ SendToServer(\"{0}\", \"Click\", \"{1}\", null); }}";
        protected string _CLICK_CLIENT_NAME = "click";

        public MenuItem( object baseObject )
            : base( baseObject )
        {
            _mi = ( Ui.ToolBar.MenuItem ) baseObject;
        }

        #region IRenderer Members

        public override void Render()
        {
            if ( !Rendered )
            {
                SetAttribute( ControlNode, "hasItems", _mi.HasDropDownItems );
                SetAttribute( ControlNode, "idx", _mi.Owner.Items.IndexOf( _mi ) );
                SetAttribute( "isInContainer", true );

                if ( _mi.HasDropDownItems )
                {
                    SetAttribute( "isContainer", true );
                }

                EnsureParentNodePosition();

                SetConstructionProperty( "text", _mi.Text );

                if ( !string.IsNullOrEmpty( _mi.ToolTipText ) )
                    SetConstructionProperty( "tooltip", _mi.ToolTipText );

                if ( _mi.CheckOnClick )
                {
                    ClassName = "Ext.menu.CheckItem";
                    _mi.ConfigOptions.Xtype = "menucheckitem";
                    if ( _mi.Checked )
                    {
                        SetConstructionProperty( "checked", _mi.Checked );
                    }

                    _mi.CheckedChanged += MenuItem_CheckedChanged;
                }

                if ( _mi.Image != null )
                {
                    SetConstructionProperty( "cls", "x-btn-text-icon" );
                    SetConstructionProperty( "icon", "webapp/Images.ashx?_id=" + InternalID );
                }

                if ( _mi.HasClick )
                {
                    SetConstructionProperty( "handler", "NetJsWire.ClickHandler" );
                    Events.Add( "Click" );
                }
            }
            base.Render();
        }

        #endregion

        private void MenuItem_CheckedChanged( object sender, EventArgs e )
        {
            _mi.Checked = ( bool ) sender;
        }

    }
}